# This file is part of the Net::Tube module.
#
# Net::Tube is Free Software; you can use, redistribute it and/or
# modify it under the terms of either:
#
# a) the GNU General Public License as published by the Free Software
#    Foundation, either version 2, or (at your opinion) any later
#    version, or
#
# b) the "Artistic License" which comes with Perl.
#
use strict;
use warnings;
use Test::Simple tests => 2;

use Net::Tube;
use Net::Tube::Loader::File;
use Net::Tube::Filter::SemanticMerge;
use Net::Tube::Filter::Sort::Date;
use Net::Tube::Filter::Limit;

use XML::LibXML;

sub test_merge {
    my $m = Net::Tube::Machine->new();
    my $c = Net::Tube::Loader::File->new(
        {
            loadSource => [
                'tdata/test_feed_atom_simple1.xml',
                'tdata/test_feed_atom_simple4.xml'
            ]
        }
    );
    my $sm     = Net::Tube::Filter::SemanticMerge->new;
    my $atomns = "http://www.w3.org/2005/Atom";
    $sm->set_config_entry( 'mergeElements', "//*[local-name() = 'entry']" );

    my $l = Net::Tube::Filter::Limit->new(
        {
            limit            => 2,
            'element-filter' =>
              "//*[namespace-uri() = '$atomns' and local-name() = 'entry']"
        }
    );

    my $sd = Net::Tube::Filter::Sort::Date->new(
        {
            'sort-element' => "//*[local-name() = 'entry']",
            'ref-element'  => "*[namespace-uri() = '$atomns' and local-name() = 'published']",
            'direction' => 'descending'      
        }
    );

    my $data;
    eval { $data = $m->execute( [ $c, $sm, $sd, $l ] ); };

    if ($@) {
        print "# and error occored: $@\n";
        return 0;
    }

    my $p     = XML::LibXML->new();
    my $td    = $p->parse_string($data);
    my @nodes =
      $td->findnodes(
        "//*[namespace-uri() = '$atomns' and local-name() = 'entry']");

    unless ( scalar @nodes == 2 ) {
        print "# data not correctly merged, got wrong number of nodes "
          . scalar(@nodes);
        print "\n";
        return 0;
    }

    @nodes =
      $td->findnodes(
"//*[namespace-uri() = '$atomns' and local-name() = 'entry']/*[namespace-uri() = '$atomns' and local-name() = 'title']"
      );

    unless ( scalar @nodes == 2 ) {
        print
"# data not correctly merged, got wrong number of nodes for the title "
          . scalar(@nodes);
        print "\n";
        return 0;
    }

    unless ( $nodes[1]->textContent eq
        'dotSUB: Multilinguale Untertitelung von Videoinhalten' )
    {
        print "# received a wrong title at the first node got '"
          . $nodes[1]->textContent . "'";
        print "\n";
        return 0;
    }

    unless ( $nodes[0]->textContent eq 'Educating People' ) {
        print "# received a wrong title at the second node got "
          . $nodes[0]->textContent;
        print "\n";
        return 0;
    }
    return 1;
}

ok( test_merge(), "merge" );


sub test_merge2 {
    my $m = Net::Tube::Machine->new();
    my $c = Net::Tube::Loader::File->new(
        {
            loadSource => [
                'tdata/test_feed_rss2_1.xml',
                'tdata/test_feed_atom_2.xml'
            ]
        }
    );
    my $sm     = Net::Tube::Filter::SemanticMerge->new({
        mergeElements => "//*[local-name() = 'entry' or local-name() = 'item']",
    });
    my $atomns = "http://www.w3.org/2005/Atom";
    
    my $l = Net::Tube::Filter::Limit->new(
        {
            limit            => 7,
            'element-filter' =>
              "//*[local-name() = 'entry' or local-name() = 'item']"
        }
    );

    my $sd = Net::Tube::Filter::Sort::Date->new(
        {
            'sort-element' => "//*[local-name() = 'entry' or local-name() = 'item']",
            'ref-element'  => "*[(namespace-uri() = '$atomns' and local-name() = 'published') or local-name() = 'pubDate']",
            'direction' => 'descending'      
        }
    );

    my $data;
    eval { $data = $m->execute( [ $c, $sm, $sd, $l ] ); };

    if ($@) {
        print "# and error occored: $@\n";
        return 0;
    }

    my $p     = XML::LibXML->new();
    my $td    = $p->parse_string($data);
    
    #warn $td->serialize(1);
    #return 0;
    
    my @nodes =
      $td->findnodes(
        "//*[local-name() = 'link']");

    unless ( scalar @nodes == 7 ) {
        print "# data not correctly merged, got wrong number of nodes "
          . scalar(@nodes);
        print "\n";
        return 0;
    }

    return 1;
}

ok( test_merge2(), "merge 2" );
